﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace RootfusBuiltinAddins.TFile
{
    /// <summary>
    /// Interaction logic for TFileUIConfig.xaml
    /// </summary>
    public partial class TFileUIConfig : UserControl
    {


        public ObservableCollection<string> ROOTFileList
        {
            get { return (ObservableCollection<string>)GetValue(ROOTFileListProperty); }
            set { SetValue(ROOTFileListProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ROOTFileList.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ROOTFileListProperty =
            DependencyProperty.Register("ROOTFileList", typeof(ObservableCollection<string>), typeof(TFileUIConfig),
            new UIPropertyMetadata(null));

        public TFileUIConfig()
        {
            ROOTFileList = new ObservableCollection<string>();
            InitializeComponent();

            ///
            /// Some dummy data
            /// 

            if (IsInDesignMode())
            {
                ROOTFileList.Add(@"c:\program files\rootfile1.root");
                ROOTFileList.Add(@"c:\user\linux\rootfile2.root");
            }
            
        }
        private bool IsInDesignMode()
        {
            return (bool)DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty,
                typeof(FrameworkElement)).Metadata.DefaultValue;
        }

        /// <summary>
        /// We keep this out here, and around, because it will allow us to keep track of things
        /// like the last directory that was opened.
        /// </summary>
        static Microsoft.Win32.OpenFileDialog sd = null;

        /// <summary>
        /// New file...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddNewFile_Click(object sender, RoutedEventArgs e)
        {
            if (sd == null)
            {
                sd = new Microsoft.Win32.OpenFileDialog();
                sd.AddExtension = true;
                sd.DefaultExt = ".root";
                sd.FileName = "";
                sd.Filter = "ROOT Files(*.root)|*.root|All files|*.*";
                sd.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                sd.Title = "Add ROOT document...";
                sd.CheckFileExists = true;
                sd.Multiselect = true;
            }

            if (sd.ShowDialog().Value)
            {
                foreach (var f in sd.FileNames)
                {
                    ROOTFileList.Add(f);
                }
            }
        }

        /// <summary>
        /// Get rid of a file!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonRemove_Click(object sender, RoutedEventArgs e)
        {
            ROOTFileList.Remove((string)((Button)sender).CommandParameter);
        }
    }
}
